﻿using OpenReport.Views;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Markup;
using System.Xml.Linq;

namespace WpfApp1
{
    /// <summary>
    /// Interaction logic for DraggableButton.xaml
    /// </summary>
    public partial class ConfigUIButton : UserControl
    {
        public ConfigUIButton()
        {
            InitializeComponent();
        }

        private List<Control> _controls = new  List<Control>();

        internal void AddControls(Control[] controls)
        {
            _controls.AddRange(controls);
        }

        public void AddControls(StackPanel continerPanel)
        {
            //var inner = (continerPanel.Children[0]) as UserControl;
            foreach (var item in continerPanel.Children)
            {
                if (item != null
                    && (item as Control) != null
                    && !_controls.Exists(r => r.Name == (item as Control).Name))
                {
                    _controls.Add(item as Control);
                }
            }
        }

        public void AddControls(ReportContiner continerPanel)
        {
            foreach (var item in (continerPanel.Content as StackPanel).Children)
            {
                if (item != null&&item is Grid)
                {
                    _controls.Add((item as Grid).Children[0] as Control);
                }
                else if (item != null && item is UserControl)
                {
                    _controls.Add(item as Control);
                }
            }
        }

        public void AddControlsatConvas(ReportContiner continerPanel)
        {
            foreach (var item in (continerPanel.Content as Canvas).Children)
            {
                if (item != null && item is Grid)
                {
                    _controls.Add((item as Grid).Children[0] as Control);
                }
                else if (item != null && item is UserControl)
                {
                    _controls.Add(item as Control);
                }
            }
        }

        private void TbtnConfig_Click(object sender, RoutedEventArgs e)
        {
            tbtnConfig.ContextMenu.IsOpen = true;
        }

        public void LoadConfigXML(string configXML)
        {
            var element = XElement.Parse(configXML);

            foreach(var el in element.Elements())
            {
                //this should be 1st-level decendants
                Control configuredControl  = XamlReader.Parse(el.ToString()) as Control;

                var win = Window.GetWindow(this);
                
                configuredControl.LoadConfiguration(win);
            }
        }

        public void LoadConfigXML(string configXML,StackPanel continer)
        {
            var element = XElement.Parse(configXML);
           
            //foreach (var el in element.Elements())
            //{
            //    Control configuredControl = XamlReader.Parse(el.ToString()) as Control;
            //    continer.Children.Add(configuredControl);
            //}

            foreach (var el in element.Elements())
            {
                Control configuredControl = XamlReader.Parse(el.ToString()) as Control;
                var win = Window.GetWindow(this);
                configuredControl.LoadConfiguration(win);
            }

            //continer.Children.Add(CloneControl(win.FindName("ElementContainer") as StackPanel));
        }
        public T CloneControl<T>(T control) where T : UIElement, new()
        {
            string xaml = XamlWriter.Save(control);
            T rtb2 = XamlReader.Parse(xaml) as T;
            return rtb2;
        }

        public string GetConfigXML()
        {
            if (_controls == null) return null;

            var configElement = new XElement("ConfiguredControl", new XAttribute("name", Name), new XAttribute("type", "xaml"));

            foreach (var ctrl in _controls)
            {
                var parentElement = ctrl.Parent as FrameworkElement;
              
                if (!parentElement.AllowDrop)
                {
                    continue; // not configured
                }

                string buttonXML = XamlWriter.Save(ctrl); //parent contains the margins needed in in the positioning

                configElement.Add(XElement.Parse(buttonXML));
            }

            return configElement.ToString();
        }

        private void EditControl(bool editing =false)
        {
            if (_controls == null) return;

            //editing = false;

            //if (editMenuItem.Header.ToString() == "Edit Layout")
            //{
            //    editMenuItem.Header = "Stop Editing";
            //    editing = true;
            //}
            //else
            //{
            //    editMenuItem.Header = "Edit Layout";
            //    editing = false;
            //}

            EditMode(editing);
        }

        private void editMenuItem_Click(object sender, RoutedEventArgs e)
        {
            EditControl(true);
        }

        public void EditMode(bool editing)
        {
            foreach (var ctrl in _controls)
            {
                if (ctrl!=null)
                    ctrl.EditMode(editing);
            }
        }

    }
}
